﻿//using System.Xml;
using Newtonsoft.Json;
using System.IO;

namespace AudioMonito
{
    public static class DataJsonTool
    {
        private static JsonSerializerSettings settings = new JsonSerializerSettings
        {
            TypeNameHandling = TypeNameHandling.Auto,
            Formatting = Formatting.Indented,
        };

        /// <summary>
        /// 读取文件夹下的所有存档文件
        /// </summary>
        /// <returns></returns>
        public static T Read<T>(string filePath, string fileName)
        {
            string json;
            using (StreamReader sr = new StreamReader($"{filePath}/{fileName}"))
            {
                json = sr.ReadToEnd();
                sr.Close();
            }
            T res = JsonConvert.DeserializeObject<T>(json, settings);
            return res;
        }


        /// <summary>
        /// 保存存档
        /// </summary>
        /// <returns></returns>
        public static void Write<T>(string filePath, string fileName, T obj)
        {
            string json = JsonConvert.SerializeObject(obj, settings);
            using (StreamWriter sw = new StreamWriter($"{filePath}/{fileName}"))
            {
                sw.WriteLine(json);
                sw.Close();
            }
        }
    }
}
